package com.zhiqingchun.web.article;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhiqingchun.dao.article.IArticleDao;
import com.zhiqingchun.dao.comment.CommentResult;
import com.zhiqingchun.dao.comment.ICommentDao;
import com.zhiqingchun.pojo.ArticleDO;
import com.zhiqingchun.pojo.Usr;

@Controller
@RequestMapping("/article.html")
public class ArticleController {

	@RequestMapping(method = RequestMethod.GET)
	public String article(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		ArticleDO articleDO = articleDao.byId(id);
		map.addAttribute("article", articleDO);
		
		// 处理收藏
		Usr usr = (Usr) request.getSession().getAttribute("user");
		String  isCollect = "no"; 
		if (usr != null ) {
			if (usr.getId() != articleDO.getAuthor()) { //当前登陆用户不是作者
				if(articleDao.isCollect(id, usr.getId()))
					isCollect = "yes"; //已收藏
			} else {
				isCollect = "self"; //当前登陆用户是作者
			}
		}
		map.addAttribute("isCollect", isCollect);
		
		// 处理评论
		CommentResult commentResult = commentDao.byId(id);
		if (commentResult.getCommentList() != null) {
			map.addAttribute("commentList", commentResult.getCommentList());
		}
		
		return "article/article";
	}
	
	@Autowired
	private IArticleDao articleDao;
	
	@Autowired
	private ICommentDao commentDao;

	public ICommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(ICommentDao commentDao) {
		this.commentDao = commentDao;
	}

	public IArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(IArticleDao articleDao) {
		this.articleDao = articleDao;
	}
}
